package com.myapplication.interfaces;

import com.myapplication.callback.TopPageCallback;
import com.myapplication.callback.TopScollViewCallback;
import com.myapplication.http.util.Util;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2016/6/22.
 */
public class TopPageConnect implements TopPageInter,TopScollViewInter {
    @Override
    public void getDataForTopPage(final TopPageCallback topPageCallback) {
        RequestParams requestParams = new RequestParams(Util.UriTopPageInfo);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                topPageCallback.onTopPageSuccessCallback(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(ex instanceof HttpException){
                    int code = ((HttpException) ex).getCode();
                    String errorResult = ((HttpException) ex).getResult();
                    topPageCallback.onTopPageFailCallback(code+errorResult);
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    @Override
    public void getTopScoll(String client, final TopScollViewCallback topScollViewCallback) {
        RequestParams params = new RequestParams(Util.UrlViewPager);
        params.addBodyParameter("client",client);
        params.setCacheDirName("ABS");
        params.setCacheMaxAge(5*1000*30);
        params.getCacheDirName();
        x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                topScollViewCallback.onScollViewCallbackSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                if(ex instanceof HttpException){
                    int code = ((HttpException) ex).getCode();
                    String errorResult = ((HttpException) ex).getResult();
                    topScollViewCallback.onScollViewCallbackFail(code+errorResult);
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
